package com.fatmeraid.controller.user;

import com.fatmeraid.common.result.Result;
import com.fatmeraid.domain.User;
import com.fatmeraid.service.UserService;
import com.fatmeraid.common.model.PageResult;
import com.fatmeraid.vo.PaginationVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;

	@Operation(summary = "获取用户列表")
	@GetMapping("/list")
	public Result<PageResult<User>> getUserList(PaginationVo paginationVo) {
		PageResult<User> userList = userService.getUserList(paginationVo);
		return Result.ok(userList);
	}

	@Operation(summary = "根据ID查询用户")
	@GetMapping("/{userId}")
	public Result<User> getUserById(@PathVariable Long userId) {
		User user = userService.getUserById(userId);
		return Result.ok(user);
	}

	@Operation(summary = "添加用户")
	@PostMapping
	public Result<Boolean> addUser(@RequestBody User user) {
		boolean result = userService.addUser(user);
		if (!result){
			return Result.fail();
		}
		return Result.ok();
	}

	@Operation(summary = "更新用户")
	@PutMapping
	public Result<Boolean> updateUser(@RequestBody User user) {
		boolean result = userService.updateUser(user);
		if (!result){
			return Result.fail();
		}
		return Result.ok();
	}

	@Operation(summary = "删除用户")
	@DeleteMapping("/{userId}")
	public Result<Boolean> deleteUser(@PathVariable Long userId) {
		boolean result = userService.deleteUser(userId);
		if (!result){
			return Result.fail();
		}
		return Result.ok();
	}
}